package com.example.gcmtest1;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.MediaController;
import android.widget.SeekBar;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.support.v4.app.NavUtils;

public class MediaPlayer1 extends Activity implements MediaPlayer.OnErrorListener, OnSeekBarChangeListener, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnPreparedListener, SurfaceHolder.Callback, OnVideoSizeChangedListener, MediaPlayerControl {

	private String logName = "MediaPlayer1";
	
	private SurfaceView surface;
	private SurfaceHolder surfaceHolder;
	private MediaPlayer mPlayer;
	private SeekBar seekBar;
	private MediaController ctrl;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_player1);
        
        this.seekBar = (SeekBar) this.findViewById(R.id.seekbar);
        this.seekBar.setOnSeekBarChangeListener(this);
        this.surface = (SurfaceView) this.findViewById(R.id.surfaceMediaPlayer1);
        
        this.surfaceHolder = this.surface.getHolder();
        this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        this.surfaceHolder.addCallback(this);
        

    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.mPlayer.release();
    }    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_media_player1, menu);
        return true;
    }

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Log.i(logName, "surfaceCreated()..");
		
        this.mPlayer = new MediaPlayer();
        this.mPlayer.setDisplay(this.surfaceHolder);
        try {
        	this.mPlayer.setDataSource("http://192.168.1.10/18.mp4");
        	this.mPlayer.setOnVideoSizeChangedListener(this);
        	this.mPlayer.setOnPreparedListener(this);
        	this.mPlayer.setOnBufferingUpdateListener(this);
        	this.mPlayer.prepareAsync();        	
        	
            this.ctrl = new MediaController(this);
            this.ctrl.setMediaPlayer(this);
            this.ctrl.setAnchorView(this.surface);
            this.ctrl.setEnabled(true);
            //this.ctrl.show(60000);
        	
        } catch (Exception e) {
        	Log.e(logName, e.toString());
        }
        
        Log.i(logName, "surfaceCreated() done");
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
		Log.i(logName, "onVideoSizeChanged(), width=" + width + ", height=" + height);
		//this.surfaceHolder.setFixedSize(width, height);
		//this.surface.requestLayout();
		//this.surface.invalidate();


	}

	@Override
	public void onPrepared(MediaPlayer arg0) {
		Log.i(logName, "onPrepared() is called");
		this.seekBar.setMax(this.mPlayer.getDuration());
		this.mPlayer.start();
		//this.mPlayer.pause();		
	}

	@Override
	public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
		//Log.i(logName, "onBufferingUpdate() is called, percentage:" + arg1);
		
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		Log.i(logName, "onSeekBarChange: current=" + progress + ", fromUser=" + fromUser);
		this.mPlayer.seekTo(progress);
		
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		
		
	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		// TODO Auto-generated method stub
		Log.e(logName, "MediaPlayer error, what=" + what + ", extra=" + extra);
		return false;
	}

	@Override
	public boolean canPause() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean canSeekBackward() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean canSeekForward() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getBufferPercentage() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getCurrentPosition() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getDuration() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean isPlaying() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void seekTo(int pos) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void start() {
		// TODO Auto-generated method stub
		
	}

    
}
